//
//  SLSwitchView.m
//  Objective-C
//
//  Created by S_LINE on 2019/9/25.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLSwitchView.h"

@interface SLSwitchView ()

@property (nonatomic, weak) UIButton *currentSelectedButton;
@property (nonatomic, strong) NSMutableArray *buttons;

@end

@implementation SLSwitchView

- (void)renderSelf {
    if (self.frame.size.width == 0 || self.frame.size.height == 0) {
        [self removeAllSubviews];
        return;
    }
    
    if (![self.titles isKindOfClass:[NSArray class]] || self.titles.count == 0) {
        [self removeAllSubviews];
        return;
    }
    
    [self removeAllSubviews];
    self.buttons = [NSMutableArray new];
    
    NSInteger count = self.titles.count;
    CGFloat buttonWidth = self.frame.size.width / count;
    CGFloat buttonHeight = self.frame.size.height;
    CGFloat buttonY = 0;
    for (int i = 0; i < self.titles.count; i++) {
        NSString *title = [self.titles objectAtIndex:i];
        
        CGFloat buttonX = i * buttonWidth;
        CGRect buttonFrame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
        
        UIButton *titleButton = [[UIButton alloc] initWithFrame:buttonFrame];
        titleButton.tag = 1000 + i;
        [titleButton setTitle:title forState:UIControlStateNormal];
        [titleButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [self addSubview:titleButton];
        [self.buttons addObject:titleButton];
        if (i == 0) {
            titleButton.selected = YES;
            self.currentSelectedButton = titleButton;
        }
    }
}

- (void)buttonClick:(UIButton *)sender {
    NSInteger currentIndex = sender.tag - 1000;
    if (currentIndex == self.currentIndex) {
        return;
    }
    if (currentIndex >= self.buttons.count) {
        return;
    }
    
    self.currentIndex = currentIndex;
    if (self.currentIndexWillChange) {
        self.currentIndexWillChange(self.currentIndex);
    }
}

- (void)setAllButtonsTextColor:(UIColor *)textColor forState:(UIControlState)state {
    for (UIButton *button in self.buttons) {
        if ([button isKindOfClass:[UIButton class]]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [button setTitleColor:textColor forState:state];
            });
        }
    }
}

#pragma mark - setter & getter
- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    
    [self renderSelf];
}

- (void)setTitles:(NSArray<NSString *> *)titles {
    _titles = titles;
    
    [self renderSelf];
}

- (void)setNormalColor:(UIColor *)normalColor {
    _normalColor = normalColor;
    
    [self setAllButtonsTextColor:normalColor forState:UIControlStateNormal];
}

- (void)setHighlightedColor:(UIColor *)highlightedColor {
    _highlightedColor = highlightedColor;
    
    [self setAllButtonsTextColor:highlightedColor forState:UIControlStateHighlighted];
}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    _currentIndex = currentIndex;
    
    if (currentIndex == self.currentSelectedButton.tag - 1000) {
        return;
    }
    
    UIButton *nextSelectButton = nil;
    if (currentIndex < self.buttons.count) {
        nextSelectButton = [self.buttons objectAtIndex:currentIndex];
    }
    [UIView animateWithDuration:0.5 animations:^{
        self.currentSelectedButton.selected = NO;
        nextSelectButton.selected = YES;
    } completion:^(BOOL finished) {
        self.currentSelectedButton = nextSelectButton;
    }];
}

@end
